package com.sskj.mine.util;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.util.ToastUtil;
import com.sskj.mine.R;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;

public class TipMineUtil {
    public static MaterialDialog getTip(Context context, String content, OnTipSure onTipSure) {
        return getTip(context, content, App.INSTANCE.getString(R.string.mineTipUtil0), onTipSure);
    }

    public static MaterialDialog getTip(Context context, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(App.INSTANCE.getString(R.string.mineTipUtil1))
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.mineTipUtil2))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static MaterialDialog getTip(Context context, String title, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .positiveText(sureMsg)
                .negativeText(App.INSTANCE.getString(R.string.mineTipUtil2))
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static void getShowAboutUS(Context context, String title, String content, String copy
            , OnTipSure onTipSure) {

        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.mine_dialog_tip, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((TextView) customeView.findViewById(R.id.tvTitle)).setText(title);
        ((TextView) customeView.findViewById(R.id.tvContent)).setText(content);
        ((TextView) customeView.findViewById(R.id.tvSure)).setText(copy);
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
    }

    public static void getGoogleCheck(Context context
            , String title, String hintContent, OnTipPwdSure onTipPwdSure) {

        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.mine_dialog_google_check, false)
                .autoDismiss(false)
                .backgroundColor(Color.TRANSPARENT)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        EditText etPwd = customView.findViewById(R.id.etPwd);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView tvCancel = customView.findViewById(R.id.tvCancel);
        tvTitle.setText(title);
        etPwd.setHint(hintContent);

        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        ClickUtil.click(customView.findViewById(R.id.btSubmit), () -> {
            String code = etPwd.getText().toString();
            if (TextUtils.isEmpty(code)) {
                ToastUtil.showShort("请输入您的谷歌验证码");
                return;
            }
            onTipPwdSure.onSure(etPwd.getText().toString());
        });
        ClickUtil.click(customView.findViewById(R.id.viewCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(tvCancel, () -> {
            dialog.dismiss();
        });

    }

    //设置商家昵称

    public static void setShopName(Context context, OnTipPwdSure onTipPwdSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.mine_dialog_google_check, false)
                .autoDismiss(false)
                .backgroundColor(Color.TRANSPARENT)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((ViewGroup) (customeView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customeView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        EditText etPwd = customeView.findViewById(R.id.etPwd);
        etPwd.setHint("请设置您的商家昵称");
        ClickUtil.click(customeView.findViewById(R.id.btSubmit), () -> {
            String shopName = etPwd.getText().toString();
            if (TextUtils.isEmpty(shopName)) {
                ToastUtil.showShort("请输入商家昵称");
                return;
            }
            onTipPwdSure.onSure(etPwd.getText().toString());
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.viewCancel), () -> {
            dialog.dismiss();
        });

    }

    public interface OnTipSure {
        void onSure();
    }

    public interface OnTipPwdSure {
        void onSure(String pwd);
    }
}
